'''
    Pegaso
    Copyright (C) 2011  Dario Tognacci

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

from modgrammar import ParseError
import sys


usage = """
Usage: pegaso [OPZIONE]...

-h, --header\tfile\t\tspecifica il file di input
    --print-xml\tfile\t\tscrive nel file l'xml del file in input
"""

# Common infos.
version = 1

# Removed line from the input file. Useful for a correct error debugging.
removed_lines = 0


# Indicates if the current position of the cursor throgh the file is in a
# comment.
in_comment = False

def remove_multiline_comment(s):
    global in_comment
    start_comment = s.find("/*")
    end_comment = s.find("*/")
    if start_comment != -1 and end_comment != -1:
        is_trasformptr_directive = s.find("@trasformPtr",start_comment,end_comment)
        is_sizeof_directive = s.find("@sizeOf",start_comment,end_comment)
        is_modifiable = s.find("@mod",start_comment,end_comment)
        if is_trasformptr_directive != -1 or is_sizeof_directive != -1 or is_modifiable != -1:
            return s
        else:
            return s[:start_comment] + s[end_comment+2:]
    elif start_comment != -1:
        in_comment = True
        return s[:start_comment] + "\n"
    elif end_comment != -1:
        in_comment = False
        return s[end_comment+2:]
    elif in_comment:
        return "\n"
    else:
        return s


def manage_error(exception, info=""):
    global removed_lines
    error_str = ""
    if exception is None:
        error_str = info
    elif isinstance(exception, ParseError):
        error_str = ("PARSING ERROR [line {0}, column {1}]: {2} {3}".format(
                    (exception.line + removed_lines, exception.col,
                    exception.message, info)))
    elif isinstance(exception, IOError):
        error_str = "INPUT/OUTPUT ERROR: {0}".format(info)
    else:
        error_str = "UNKOWN ERROR!"

    # Maybe could be a better solution deleting the previous files created.
    sys.exit(error_str)


class TabWriter(object):
    
    def __init__(self, file):
        self.file = file
        self.level = 0
    
    def write(self, iter):
        for item in iter:
            if item[-1] == "{":
                self.file.write("{0}{1}\n".format("\t" * self.level, item))
                self.level = self.level + 1
            elif item[-1] == "}":
                self.level = self.level - 1
                self.file.write("{0}{1}\n".format("\t" * self.level, item))
            else:
                self.file.write("{0}{1}\n".format("\t" * self.level, item))
    
    def close(self):
        self.file.close()
        
    
